// pages/meeting/meetingorder.js

Page({
  mixins: [require('../mixin/themeChanged')],

  /**
   * 页面的初始数据
   */
  data: {
    showmark: 0,
    date: "请选择",
    meetingroom: ["请选择"],
    meetingroomindex: 0,
    meetinginfo: {},
    stime: "请选择",
    etime: "请选择",
    meetingname: "",
    city: "",

    list: [{
      id: '1',
      name: '查看已预约会议信息',
      pages: [{
        id: "1",
        name: "测试会议",
        url: "../meeting/meetingorder"
      }]
    }, ],


  },

  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
    var that = this
    var url = wx.getStorageSync("config")
    wx.request({
      url: url.service.homeUrl + '/meeting/lookupmeeting',
      data: {
        meetingroom: that.data.meetingroom[that.data.meetingroomindex],
        date: that.data.date,
      },
      // header: {
      //   Authorization: userInfo ? "token " + userInfo.token : ""
      // },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        this.setData({
          meetinginfo: res.data,
        })
      },
      fail: function (res) {},
      complete: function (res) {},
    })
  },

  bindStimeChange: function (e) {
    this.setData({
      stime: e.detail.value
    })
  },

  bindEtimeChange: function (e) {
    this.setData({
      etime: e.detail.value
    })
  },

  getMeetingName: function (e) {
    this.setData({
      meetingname: e.detail.value
    })
  },


  bindMeetingroomChange: function (e) {
    this.setData({
      meetingroomindex: e.detail.value
    })
    var that = this
    var url = wx.getStorageSync("config")
    wx.request({
      url: url.service.homeUrl + '/meeting/lookupmeeting',
      data: {
        meetingroom: that.data.meetingroom[that.data.meetingroomindex],
        date: that.data.date,
      },
      // header: {
      //   Authorization: userInfo ? "token " + userInfo.token : ""
      // },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        this.setData({
          meetinginfo: res.data,
        })
        this.setData({
          showmark: 1,
        })
      },
      fail: function (res) {},
      complete: function (res) {},
    })
  },


  postMeetingOrder: function () {
    var that = this
    var url = wx.getStorageSync("config")
    var uid = wx.getStorageSync('uid')


    // wx.showModal({
    //   title: '预约确认',
    //   content: '确定预约该会议吗',
    //   success: function (res) {
    //     if (res.confirm) {

          wx.request({
            url: url.service.homeUrl + '/meeting/meetingorder',
            data: {
              meetingroom: that.data.meetingroom[that.data.meetingroomindex],
              meetingdate: that.data.date,
              name: that.data.meetingname,
              stime: that.data.stime,
              etime: that.data.etime,
              uid: uid,
              city:that.data.city,
            },

            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: (res) => {
              wx.showToast({
                title: res.data.msg,
                icon: "none"
              })

              //刷新会议室预约信息
              wx.request({
                url: url.service.homeUrl + '/meeting/lookupmeeting',
                data: {
                  meetingroom: that.data.meetingroom[that.data.meetingroomindex],
                  date: that.data.date,
                },
                method: 'GET',
                dataType: 'json',
                responseType: 'text',
                success: (res) => {
                  this.setData({
                    meetinginfo: res.data,
                  })
                  this.setData({
                    showmark: 1,
                  })

                },
                fail: function (res) {},
                complete: function (res) {},
              })


              //刷新已约会议信息

              var thing = "list[0].pages"
              wx.request({
                url: url.service.homeUrl + '/meeting/getmeetingorderlist',
                data: {
                  uid: wx.getStorageSync('uid')
                },
                method: 'GET',
                dataType: 'json',
                responseType: 'text',
                success: (res) => {this.setData({[thing]: res.data})},
                fail: function (res) {},
                complete: function (res) {},
              })

            },
            fail: function (res) {
              wx.showToast({
                title: res.data.msg,
              })
            },
            complete: function (res) {},
          })
   
    //     }
    //   }
    // })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var url = wx.getStorageSync("config")
    this.setData({
      city: wx.getStorageSync('city')
    })
    wx.request({
      url: url.service.homeUrl + '/meeting/getmeetingroom',
      data: {
        city: that.data.city
      },

      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        for (var i = 0; i < res.data.length; i++) {
          var newarray = [res.data[i].meetingroom, ];
          that.data.meetingroom = that.data.meetingroom.concat(newarray);
          that.setData({
            meetingroom: that.data.meetingroom
          });
        }
      },
      fail: function (res) {},
      complete: function (res) {},
    });


    var thing = 'list[0].pages'
    wx.request({
      url: url.service.homeUrl + '/meeting/getmeetingorderlist',
      data: {
        uid: wx.getStorageSync('uid')
      },

      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        this.setData({
          [thing]: res.data
        })
        console.log(that.data.list)
      },
      fail: function (res) {},
      complete: function (res) {},
    })



  },

  kindToggle: function (e) {
    var id = e.currentTarget.id,
      list = this.data.list;
    for (var i = 0, len = list.length; i < len; ++i) {
      if (list[i].id == id) {
        list[i].open = !list[i].open
      } else {
        list[i].open = false
      }
    }
    this.setData({
      list: list
    });
  },

  changeTheme: function () {
    console.log(this.data);
    getApp().themeChanged(this.data.theme === 'light' ? 'dark' : 'light');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})